/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;

/**
 * @author Administrador
 */
public class PruebaAudioTiempos extends MIDlet implements CommandListener {

    private Command exitCommand, playCommand, stopCommand;
    private Display display;
    private Form screen;
    private Reproductor myPlayer;
    

    public PruebaAudioTiempos(){
        try {
            initUI();
            myPlayer = new Reproductor();
        } catch (Exception ex) {manageException(ex);}
    }

    public void startApp() {
        
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    private void initUI() {
        display = Display.getDisplay(this);

        screen = new Form("PruebaAudioTiempos");

        exitCommand = new Command("Salir", Command.EXIT, 2);
        playCommand = new Command("Play", Command.SCREEN, 0);
        stopCommand = new Command ("Pausa", Command.SCREEN, 1);

        screen.addCommand(exitCommand);
        screen.addCommand(playCommand);

        screen.setCommandListener(this);

        display.setCurrent(screen);

    }

    public void commandAction(Command c, Displayable d) {
        if (c==exitCommand){
            destroyApp(false);
            notifyDestroyed();
        }else if (c==playCommand){
            screen.removeCommand(c);
            screen.addCommand(stopCommand);
            try {
                myPlayer.run();
            } catch (Exception ex) { manageException(ex); }
            }else try{
                    myPlayer.pause();
                    screen.removeCommand(c);
                    screen.addCommand(playCommand);
                  }catch(Exception e){manageException(e);}
    }

    private void manageException(Exception e){
        Alert aux=new Alert("Exception", e.toString(), null, null);
        aux.setTimeout(Alert.FOREVER);
        display.setCurrent(aux);
        e.printStackTrace();
    }

    private void say(String mensaje){
        screen.append(new StringItem("","\n"+mensaje));
    }
}
